This page last changed on Jul 28, 2006 by stephen fenech.

In this Example we are going to configure Mule to monitor a certain folder (for example an incoming FTP folder) and once a file is received, we move it to another location and send an email notifying that a file has been received. In our case we are going to assume that we are receiving files in the folder ./files/incoming and then moving them to ./files/done.

There are a number of ways of how this can be done, but in this case we are going to make use of the Mule API.

We can start off by configuring the File Connector, which will effectively trigger our Component.

<connector name="myFileConnector" className="org.mule.providers.file.FileConnector">
	<properties>
		<property name="moveToDirectory" value="files/done"/>
		<property value="${SYSTIME}_${ORIGINALNAME}" name="moveToPattern"/>
	</properties>
</connector>

This connector has 2 properties, the moveToDirectory which specifies to which directory a file should be moved to once it is read, and the moveToPattern which specifies the filename of the moved file. In our case we added a System Time value to the Original name separated by an "_" so that we don't overwrite previously received files and also we are effectively time stamping these files. Check out the File Provider page in order to see all the properties available.

We are now going to set up the Email Connector:

<connector name="EmailConnector" className="org.mule.providers.email.SmtpConnector">
	<properties>
		<property value="[email protected]" name="fromAddress"/>
		<property value="symphonysoft.com" name="hostname"/>
		<property value="myusername" name="username"/>
		<property value="mypassword" name="password"/>
		<property value="File Received!" name="subject"/>
	</properties>
</connector>

For the email connector we have a number of properties to set as seen above. Check out the Smtp Provider page for all the details.

We can now turn to our java component which will do the actual work.

public class NotifyFileReceivedSmtpAPI implements Callable{

	private String emailTextFile;
	private TemplateParser parser = TemplateParser.createAntStyleParser();
	private String emailText;


	public Object onCall(UMOEventContext eventContext) throws Exception {
		UMOMessage msg;

		msg=eventContext.getMessage();
		Map props = new HashMap();

        	//Will replace ${filename} in the email text
        	props.put("filename", msg.getProperty("originalFilename"));

		return parser.parse(props, emailText);
	}

	private String readFile(String path) throws IOException
	{
		FileReader fr = new FileReader(path);
		BufferedReader br=new BufferedReader(fr);

		String tmp;
		String email="";
		while((tmp=br.readLine())!=null)
		{
			email=email.concat(tmp)+"\r\n";
		}

		return email;
	}

	public String getEmailTextFile() {
		return this.emailTextFile;
	}

	public void setEmailTextFile(String emailTextFile) throws IOException {
		this.emailTextFile = emailTextFile;
		this.emailText=this.readFile(this.emailTextFile);
	}

}

The first thing to note is that we are implementing the Callable interface. This provides us with the onCall method. We should also note the private String emailText with its getters and setters. This value will hold the path of the text file to open in order to get the email text. We will set this value through the Mule configuration. This code will simply read the file, then substitute the "${filename}" template in the emailText with the message Property "originalFilename" which holds the filename of the received file.

Next we are going to identify our endpoints:

<endpoint-identifiers>
	<endpoint-identifier value="file://files/incoming" name="ServiceInbound"/>
	<endpoint-identifier value="smtp://[email protected]" name="ToSmtpServer"/>
</endpoint-identifiers>

The Service Inbound endpoint specifies in which folder we will be monitoring. Since we have just 1 file connector defined, it will be used by default (if more than 1 is defined, the connector property needs to be set...). The next endpoint specifies the SMTP server and the address to whom it should be sent.

The final step is to configure our service.

<mule-descriptor name="SmtpNotification" implementation="com.ss.examples.smtpfilerecievednotify.NotifyFileReceivedSmtpAPI">
	<inbound-router>
		<endpoint address="ServiceInbound"/>
	</inbound-router>
	<outbound-router>
		<router className="org.mule.routing.outbound.OutboundPassThroughRouter">
			<endpoint address="ToSmtpServer"/>
		</router>
	</outbound-router>
	<properties>
		<property value="email.txt" name="emailTextFile"/>
	</properties>
</mule-descriptor>

The inbound router will be listening on the file endpoint. Once a file is received, mule will move the file to the done directory, and then send a message containing the actual text in the file(in this case we are not making use of the actual file data.). The onCall method of the component will be called, which will return the actual email text. This will then be directed to the SmtpServer endpoint.

After specifying the outbound-router, we specify the properties to be set on the component. In our case we have a single property named emailText, and here we specify the file "email.txt" from where the email body will be obtained.

Document generated by Confluence on Oct 03, 2006 09:23